

* +++++++++++++++++++++
* CLEAN RELIGIOSITY DATA
* +++++++++++++++++++++

* religion data (from Census 2011)
import delimited "${data_raw}/religion/2000S-1022.csv", clear delimiter(";")

* clean up variable names etc. 
keep if _n>=6 & _n<=419

destring v1, replace force
ren (v1 v2 v3 v4 v7) (ags_old place_name pop_tot pop_catholic pop_orthodox)
destring pop_tot pop_catholic v5 v6 pop_orthodox, replace force
gen pop_protestant = v5 + v6 
replace pop_protestant = v5 if v6 ==.
replace pop_protestant = v6 if v5 ==.
gen pop_christian = pop_catholic + pop_protestant + pop_orthodox
replace pop_christian = pop_catholic + pop_protestant if pop_orthodox ==. 

ren (v8 v9) (pop_jewish pop_oth_recog_rel) 
destring pop_jewish pop_oth_recog_rel, replace force

* crosswalk to new ags codes 
keep if _n>=3
merge 1:1 ags_old using "${data_derived}/ags_old_to_new.dta", nogen assert(3)

* collapse over new ags codes
collapse (rawsum) pop* (first) place_name, by(ags)

* generate fractions
foreach rel in catholic protestant christian {
	gen frac_`rel' = pop_`rel' / pop_tot
}

* fractions of keep variables
foreach rel in catholic protestant {
	gen frac_`rel'_among_chris = pop_`rel' / pop_christian
}

* save
drop pop_tot
save "${data_derived}/religion_census_2011.dta", replace
